Java开发 - 老虎机

老虎机

image-20190307224935020

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package test.iris;

import java.awt.Color;

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

public class UI extends JFrame
{
private static UI ui = null;
static String[] paths =
{ "1.png", "2.png", "3.png" };

private MyLabel labelLeft = new MyLabel(paths[0], 0);// 左图
private MyLabel labelCenter = new MyLabel(paths[1], 1);// 中图
private MyLabel labelRight = new MyLabel(paths[2], 2);// 右图

private JButton button = new JButton("开始");// 开始按钮

private JLabel labelMessage = new JLabel("");// 结果标签

private UI()
{
setTitle("老虎机");
setBounds(200, 200, 500, 500);
JPanel panel = new JPanel();
setContentPane(panel);
panel.setLayout(new GridLayout(3, 1));
panel.setBorder(new EmptyBorder(50, 50, 50, 50));

// 上中下三块面板
JPanel panelTop = new JPanel();
JPanel panelCenter = new JPanel();
JPanel panelBottom = new JPanel();

panel.add(panelTop);
panel.add(panelCenter);
panel.add(panelBottom);

// 三张图片显示区域
panelTop.setLayout(new FlowLayout());
panelTop.add(labelLeft);
panelTop.add(labelCenter);
panelTop.add(labelRight);

// 开始按钮面板
panelCenter.setLayout(new GridLayout(1, 1));
panelCenter.setBorder(new EmptyBorder(30, 100, 30, 100));
button.addActionListener(new MyListener());
panelCenter.add(button);

// 抽奖结果面板
panelBottom.setLayout(new GridLayout(1, 1));
labelMessage.setBorder(BorderFactory.createLineBorder(Color.BLACK));
labelMessage.setFont(new Font("微软雅黑",0 , 40));
labelMessage.setForeground(Color.red);
labelMessage.setHorizontalAlignment(SwingConstants.CENTER);

panelBottom.add(labelMessage);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public static UI getInstance()
{
return UI.ui;
}

public static String[] getPaths()
{
return paths;
}

public MyLabel getLabelLeft()
{
return labelLeft;
}

public MyLabel getLabelCenter()
{
return labelCenter;
}

public MyLabel getLabelRight()
{
return labelRight;
}

public JButton getButton()
{
return button;
}

public JLabel getLabelMessage()
{
return labelMessage;
}

public static void main(String[] args)
{
UI.ui = new UI();
}
}

class MyListener implements ActionListener
{
private boolean buttonFlag = false;// 按钮点击状态,默认未点击
public static boolean threadFlag = true;//线程中断标志

@Override
public void actionPerformed(ActionEvent e)
{

UI ui = UI.getInstance();
JButton button = ui.getButton();
JLabel labelMessage = ui.getLabelMessage();

MyLabel labelLeft = ui.getLabelLeft();
MyLabel labelCenter = ui.getLabelCenter();
MyLabel labelRight = ui.getLabelRight();
if (buttonFlag)
{
threadFlag = false;
buttonFlag = false;
button.setText("开始");

synchronized (labelLeft) //等待三个线程释放锁
{
synchronized (labelCenter)
{
synchronized (labelRight)
{
int leftId = labelLeft.getId();
int centerId = labelCenter.getId();
int rightId = labelRight.getId();
String result = getResult(leftId, centerId, rightId);
labelMessage.setText(result);
}
}
}
} else
{
threadFlag = true;
buttonFlag = true;
new MyThread(220, labelLeft).start();
new MyThread(150, labelCenter).start();
new MyThread(60, labelRight).start();
labelMessage.setText("");
button.setText("暂停");
}
}

public static String getResult(int leftId, int centerId, int rightId)
{
String result = null;
if (leftId == centerId & leftId == rightId)
result = "一等奖";
else if (leftId == centerId || leftId == rightId || centerId == rightId)
result = "二等奖";
else
result = "未中奖";
return result;
}
}

class MyLabel extends JLabel
{
private int id;// 标签id

public MyLabel(String path, int id)
{
ImageIcon icon = new ImageIcon(path);
setIcon(icon);
this.id = id;
}

public int getId()
{
return id;
}

public void setId(int id)
{
this.id = id;
}

}

class MyThread extends Thread
{
private long time;
private MyLabel label;
private Random random = new Random();
public MyThread(long time, MyLabel label)
{
this.time = time;
this.label = label;
}
@Override
public void run()
{
synchronized (label) //锁住lable对象
{
while (MyListener.threadFlag)
{
try
{
Thread.sleep(time);
int id = random.nextInt(3);
label.setId(id);
ImageIcon icon = new ImageIcon(UI.paths[id]);
label.setIcon(icon);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}